
//声明 reducer 函数
let countReducer = (state=500, action) => {
  //根据 action 的操作类型, 对状态进行不同的处理
  switch(action.type){
    //减少
    case 'decre':
      return state - action.payload;
    //增加
    case 'incre':
      return state + action.payload;
    //其他情况
    default:
      return state;
  }
}

//暴露
export default countReducer;

//同步的 action creator
export let increAction = (payload) => {
  return {type: 'incre', payload: payload}
};

//声明创建 action 对象的函数 - decre
export let decreAction = payload => ({type: 'decre', payload});

//封装  异步的 action creator
export let asyncIncreAction = (payload) => {
  return (dispatch) => {
    setTimeout(() => {
      //更新 redux 中的状态
      dispatch(increAction(payload));
    }, 1000);
  }
}
